﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Web;
//using Web.Auth.Helpers;

namespace API.Models.Repository
{
    public class Users_RepositoryAsync : GenericRepositoryAsync<PartnerDbContext, SEC_USERS>
    {
        public override async Task Edit(SEC_USERS entity)
        {
            if (this.session == null)
                throw new HttpException(401, "Unauthorized access");

            var data = await this.Find(r => r.GUID_RECORD == this.session.USER_GUID);
            var user = data.FirstOrDefault();
            
            if (user == null)
                throw new Exception("Пользователь не найден");

            user.EMAIL = entity.EMAIL;
            user.FIRSTNAME = entity.FIRSTNAME;
            user.LASTNAME = entity.LASTNAME;
            user.MIDNAME = entity.MIDNAME;
            user.TIMEZONE = entity.TIMEZONE;

            await base.Edit(user);
            await this.Save();
        }

        public async Task Register(SEC_USERS user)
        {
            var data = await this.Find(r => r.EMAIL == user.EMAIL);

            if (data.Any())
                throw new Exception(String.Format("Пользователь {0} уже зарегистрирован", user.EMAIL));

            user.GUID_RECORD = Guid.NewGuid();
            user.PASSWORD_HASH = API.Core.Util.GetMD5(user.PASSWORD);

            await this.Add(user);
            await this.Save();
        }

        public async Task Register(string email, string password)
        {
            var data = await this.Find(r => r.EMAIL == email);

            if (data.Any())
                throw new Exception(String.Format("Пользователь {0} уже зарегистрирован", email));

            var user = new SEC_USERS
            {
                GUID_RECORD = Guid.NewGuid(),
                EMAIL = email,
                FIRSTNAME = String.Empty,
                LASTNAME = String.Empty,
                PASSWORD_HASH = API.Core.Util.GetMD5(password)
            };

            await this.Add(user);
            await this.Save();
        }

        public async Task ResetPassword(string email, string password)
        {
            var data = await this.Find(r => r.EMAIL == email);
            var user = data.FirstOrDefault();

            if (user == null)
                throw new Exception(String.Format("Пользователь {0} не найден", email));

            user.PASSWORD_HASH = API.Core.Util.GetMD5(password);

            await base.Edit(user);
            await this.Save();
        }

        public async Task ChangePassword(string password)
        {
            if (this.session == null)
                throw new HttpException(401, "Unauthorized access");

            var data = await this.Find(r => r.GUID_RECORD == this.session.USER_GUID);
            var user = data.FirstOrDefault();

            if (user == null)
                throw new Exception("Пользователь не найден");

            user.PASSWORD_HASH = API.Core.Util.GetMD5(password);

            await base.Edit(user);
            await this.Save();
        }
    }
}